<?php

/**
 *
 * Class LoginController
 * @author dashan
 * @date 2022-05
 */

namespace app\agent\controller;

use app\common\model\AgentModel;
use app\common\model\UserModel;
use think\facade\Request;

class LoginController extends BaseController {
    public function initialize() {

    }

    /**
     * 登录页面
     * @return mixed
     */
    public function index() {
        $token          = parent::operateCookieAgentId();
        $agentid        = parent::operateCookieAgentToken();
        $this->userinfo = UserModel::with(['agent' => function ($query) use ($token) {
            $query->where('token', $token);
        }])->where("id", $agentid)->find();
        if ($this->userinfo && !empty($this->userinfo->agent)) {
            $this->redirect(url('Index/index'));
        }
        return $this->fetch();
    }

    /**
     * 修改密码页面
     * @return mixed
     */
    public function editpwd() {
        return $this->fetch();
    }

    /**
     * 登录逻辑
     * @return \think\response\Json
     */
    public function login() {
        $mobile   = Request::param("mobile");
        $password = Request::param("password");
        $vercode  = Request::param("vercode");

        if (empty($mobile)) {
            return self::bulidFail("账号有误");
        }

        if (empty($password)) {
            return self::bulidFail("密码有误");
        }

        if (!captcha_check($vercode)) {
            return self::bulidFail("验证码有误");
        }

        $userinfo = UserModel::with('agent')->where("mobile", $mobile)->whereOr("account", $mobile)->find();
        if (empty($userinfo)) {
            return self::bulidFail("用户名或密码错误");
        }

        if ($userinfo->status != 0) {
            return self::bulidFail("您当前被禁止登陆");
        }

        if (empty($userinfo->agent)) {
            return self::bulidFail("您当前还不是代理");
        }

        // 验证密码
        $pwd = openssl_encrypt(substr($userinfo->account, strlen($userinfo->account) - 4, 4) . $password, "DES-ECB", $this->OPENSSL_KEY);

        if ($userinfo->password == $pwd) {
            // 修改代理表token

            $new_token = strtoupper(openssl_encrypt(date("YmdHis") . rand(10000, 99999), "DES-ECB", $this->OPENSSL_KEY));
            AgentModel::where('uid', $userinfo->id)->update(['token' => $new_token]);

            parent::operateCookieAgentToken($new_token);
            parent::operateCookieAgentId($userinfo->id);

            return self::bulidSuccess([], "登录成功");
        }

        return self::bulidFail("用户名或密码错误");
    }

    public function logout() {
        parent::clearAgentCookie();
        return self::bulidSuccess([], "登出成功");
    }

    /**
     * 修改密码
     * @return \think\response\Json
     */
    public function editpwd_post() {
        $agentid = parent::operateCookieAgentId();
        $token   = parent::operateCookieAgentToken();
        if ($agentid < 1 || empty($token)) {
            return self::bulidLoginTimeOut("请重新登录");
        }

        $agent_info = UserModel::with(['agent' => function ($query) use ($token) {
            $query->where('token', $token);
        }])->where("id", $agentid)->find();

        if (!$agent_info || empty($agent_info->agent)) {
            return self::bulidLoginTimeOut("您当前还不是代理");
        }

        if ($agent_info->status != 0) {
            return self::bulidLoginTimeOut("您当前被禁止登陆");
        }

        $oldpwd = openssl_encrypt(substr($agent_info->account, strlen($agent_info->account) - 4, 4) . Request::param("oldpwd"), "DES-ECB", $this->OPENSSL_KEY);
        if ($oldpwd != $agent_info->password) {
            return self::bulidFail('旧密码验证失败');
        }

        $agent_info->password = openssl_encrypt(substr($agent_info->account, strlen($agent_info->account) - 4, 4) . Request::param("newpwd"), "DES-ECB", $this->OPENSSL_KEY);
        if ($agent_info->save()) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }


}